/**
 * Copyright (c) 2012-2013 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 * See LICENSE.TXT for license information.
 */
package com.nokia.example.fuelcalculator;

import com.nokia.example.fuelcalculator.model.FuelConsumptionModel;
import com.nokia.example.fuelcalculator.utils.Persistor;
import com.nokia.example.fuelcalculator.view.SplashCanvas;
import com.nokia.example.fuelcalculator.view.ViewManager;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;

/*
 * Main class to handle starting the application, pausing it and exiting it.
 */
public class Midlet
    extends MIDlet {

    private static final String SPLASH_IMG_NAME = "/splash.png";
    private static final String SPLASH_TEXT = "Fuel Calculator";
    private static final String RECORD_STORE_NAME = "FuelConsumptionLog";
    private static final int MAX_SLEEP = 3000;
    private FuelConsumptionModel model;
    private ViewManager viewManager;
    private Persistor persistor;
    private static Midlet instance = null;

    /**
     * Initializes model and views.
     * @see javax.microedition.midlet.MIDlet#startApp()
     */
    public void startApp() {
        if (instance == null) {
            instance = this;
            showSplashScreen();
        } else {
            viewManager.showEnterView();
        }
    }

    /**
     * Pauses the app.
     * @see javax.microedition.midlet.MIDlet#pauseApp()
     */
    public void pauseApp() {
    }

    /**
     * Saves consumption log (aka the state) before destroying the application.
     * @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
     * @param unconditional Should the MIDlet cleanup and release all resources.
     */
    public void destroyApp(boolean unconditional) {
        persistor.saveState();
        notifyDestroyed();
    }
  
    /**
     * Getter for the only midlet instance. This can be used in other classes
     * to get references to all the models and managers.
     * @return Midlet instance
     */
    public static Midlet getInstance() {
        return instance;
    }

    /**
     * Getter for the model of the app.
     * @return FuelConsumptionModel 
     */
    public FuelConsumptionModel getFuelConsumptionModel() {
        return model;
    }
    
    /*
     * Show splash screen for 3 seconds and initialize class variables
     * within that time.
     */
    private void showSplashScreen() {
        Display display = Display.getDisplay(this);
        long timeOne;
        long timeTwo;
        
        SplashCanvas splash = new SplashCanvas(SPLASH_IMG_NAME, SPLASH_TEXT);
        display.setCurrent(splash);
        
        timeOne = System.currentTimeMillis();
        
        model = new FuelConsumptionModel();
        persistor = new Persistor(model, RECORD_STORE_NAME);
        persistor.loadState();
        viewManager = new ViewManager(display);
        
        timeTwo = System.currentTimeMillis();
        
        try {
            Thread.sleep(MAX_SLEEP - (timeTwo - timeOne));
        } catch (InterruptedException ex) {
           // do nothing
        }

        viewManager.showEnterView();
    }
}
